
import adb_utils
from PIL import Image
import numpy as np
import pytesseract
import time
import cv2
import re
import os
import math
import operator
from functools import reduce
from collections import namedtuple
import cv2 as cv

# 手动设置 Tesseract 路径（替换成你的实际路径）
pytesseract.pytesseract.tesseract_cmd = r'E:\game_shell\tesseract\tesseract.exe'

# 定义命名元组结构
CropCoords = namedtuple('CropCoords', ['left', 'upper', 'right', 'lower'])

def recognize_num(save_path,reco_name,coords: CropCoords):
    crop_path = (
            r"E:\game_shellPrj\AutoDerbyV1.00"
            r"\screen_shot\frome_device\{}.png".format(reco_name)
        )
    config_cmd = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789'
    img = Image.open(save_path)
    cropped = img.crop((coords.left, coords.upper, coords.right, coords.lower))  # (左，上，右，下 )
    cropped.save(crop_path)
    num_1 = Image.open(crop_path)
    print(reco_name+":"+pytesseract.image_to_string(num_1,config=config_cmd))

def saima():
    
    save_path = (
            r"E:\game_shellPrj\AutoDerbyV1.00"
            r"\screen_shot\frome_device\cultivate.png"
        )

    adb_utils.test_screenshot(save_path)
    img = cv2.imread(save_path)
    gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.imwrite(save_path,gray_image)
    print(img.shape)

    #识别速度，后期改成比例·
    coords = CropCoords(left=109, upper=1281, right=200, lower=1325)
    recognize_num(save_path,"speed",coords)

    #识别耐力，后期改成比例·
    coords = CropCoords(left=281, upper=1281, right=372, lower=1325)
    recognize_num(save_path,"stamina",coords)

    #识别力量，后期改成比例·
    coords = CropCoords(left=444, upper=1281, right=535, lower=1325)
    recognize_num(save_path,"power",coords)

    #识别毅力，后期改成比例·
    coords = CropCoords(left=611, upper=1281, right=702, lower=1325)
    recognize_num(save_path,"willpower",coords)

    #识别智力，后期改成比例·
    coords = CropCoords(left=777, upper=1281, right=868, lower=1325)
    recognize_num(save_path,"intellect",coords)